- Given a list of integers, find the sum of all elements.
- Write a program to remove all duplicates from a list of strings.
- Create a function that takes a list of numbers and returns a new list with only the even numbers.
- Given two lists, find the common elements between them.
- Implement a function to merge two sorted lists into a single sorted list.
- Create a program to find the maximum and minimum values in a list of floats.
- Write a function that takes a list of strings and returns the longest string in the list.
- Create a program to count the occurrences of each word in a given sentence using a list.
- Given a list of names, sort them in alphabetical order.
- Implement a function that takes a list of numbers and returns a new list with the squared values of each element.
- Multiply all elements in a list.
- Insert an element at a specific index in the list.
- Remove an element from the list by its value.
- Remove an element from the list by its index.
- Find the index of the first occurrence of a specific element in the list.
- Find the index of the last occurrence of a specific element in the list.
- Reverse a list.
- Check if a list is empty.
- Find the sum of all elements in a list of integers:
function findSum(list) {
return list.reduce((sum, num) => sum + num, 0);
}
- Remove duplicates from a list of strings:
function removeDuplicates(list) {
return Array.from(new Set(list));
}
- Return a new list with only the even numbers:
function filterEvenNumbers(list) {
return list.filter(num => num % 2 === 0);
}
- Find common elements between two lists:
function findCommonElements(list1, list2) {
return list1.filter(item => list2.includes(item));
}
- Merge two sorted lists into a single sorted list:
function mergeSortedLists(list1, list2) {
return [...list1, ...list2].sort((a, b) => a - b);
}
- Find maximum and minimum values in a list of floats:
function findMinMaxValues(list) {
const max = Math.max(...list);
const min = Math.min(...list);
return { max, min };
}
- Find the longest string in a list of strings:
function findLongestString(list) {
return list.reduce((longest, current) => current.length > longest.length ? current : longest, '');
}
- Count occurrences of each word in a sentence using a list:
function countWordOccurrences(sentence) {
const words = sentence.split(' ');
const wordCount = {};
words.forEach(word => {
wordCount[word] = (wordCount[word] || 0) + 1;
});
return wordCount;
}
- Sort a list of names in alphabetical order:
function sortNamesAlphabetically(names) {
return names.sort();
}
- Return a new list with squared values of each element:
function squareList(list) {
return list.map(num => num * num);
}
- Multiply all elements in a list:
function multiplyListElements(list) {
return list.reduce((product, num) => product * num, 1);
}
- Insert an element at a specific index in the list:
function insertAtIndex(list, index, element) {
list.splice(index, 0, element);
return list;
}
- Remove an element from the list by its value:
function removeByValue(list, value) {
return list.filter(item => item !== value);
}
- Remove an element from the list by its index:
function removeByIndex(list, index) {
list.splice(index, 1);
return list;
}
- Find the index of the first occurrence of a specific element:
function findFirstIndex(list, element) {
return list.indexOf(element);
}
- Find the index of the last occurrence of a specific element:
function findLastIndex(list, element) {
return list.lastIndexOf(element);
}
- Reverse a list:
function reverseList(list) {
return list.reverse();
}
- Check if a list is empty:
function isListEmpty(list) {
return list.length === 0;
}